package com.study.netty;

import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

@Component
public class ChannelHandler {
	
	@SuppressWarnings("unchecked")
	public static void addOrRefreshChannel(String imei, Channel channel) {
		Channel $channel = ChannelMap.get(imei);
		if($channel == null){
			ChannelMap.add(imei,channel);
		}else{
			if(!$channel.equals(channel)){  // 如果通道已改变
				ChannelMap.removeByKey(imei);
				ChannelMap.add(imei,channel);
			}
		}
		// 维护用户在线状态
//		boolean deviceIsOnLine = InjectService.deviceIsOnLine(imei);
//		if(deviceIsOnLine){
//			//设备在线则刷新设备在线时长
//			InjectService.refreshDeviceOnLineTime(imei);
//		}else{
//			//设备离线则存储设备在线状态
//		}
//		InjectService.setDeviceOnline(imei);
	}

	public static Channel getChannel(String imei) {
		return ChannelMap.get(imei);
	}


	public static void closeChannel(Channel channel) {
		String devicePhone = ChannelMap.getKey(channel);
		ChannelMap.removeByKey(devicePhone);
		channel.close();
		//关闭通道后需要删除redis中存储的在线状态
//		InjectService.removeOnline(devicePhone);
	}

}
